/**
 * @(#)ChannelApkInfoService.java 2016年11月11日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ConstantsCmp;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.*;
import org.jsbd.boss.domian.ChannelApkConfigTimeModel;
import org.jsbd.boss.domian.ChannelProvinceInfo;
import org.jsbd.boss.domian.RetentPayConfig;
import org.jsbd.boss.domian.channel.*;
import org.jsbd.boss.enums.ApkBaseEnum;
import org.jsbd.boss.enums.LoginTypeEnum;
import org.jsbd.boss.enums.SmsSendTypeEnum;
import org.jsbd.boss.service.FileManager;
import org.jsbd.boss.service.IApkLoginTypeService;
import org.jsbd.boss.service.IApkSimulatorService;
import org.jsbd.boss.service.IChannelApkInfoService;
import org.jsbd.boss.service.IDeviceService;
import org.jsbd.boss.service.IRetentFilterConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import edu.hziee.common.tcp.cache.CachedHolder;

/**
 * apk信息查询服务
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ChannelApkInfoServiceImpl implements IChannelApkInfoService {
	public static final Logger LOGGER = LoggerFactory.getLogger(ChannelApkInfoServiceImpl.class);

	@Resource
	private IChannelApkConfigDAO channelApkConfigDAO;

	@Resource
	private IChannelApkLimitDAO channelApkLimitDAO;

	@Resource
	private IApkLoginTypeService loginTypeService;

	@Resource
	private IApkSimulatorService apkSimulatorService;

	@Resource
	private IDeviceService deviceService;

	@Resource
	private RedisClient redisClient;
	@Resource
	private FileManager fileManager;
	@Resource
	private IChannelProvicneInfoDAO channelProvicneInfoDAO;

	@Resource
	private IApkSimulatorDAO apkSimulatorDAO;

	@Resource
	private IRetentFilterConfigService retentFilterConfigService;

	@Resource
	private CachedHolder ehcache;


	@Resource
	private IBrushApkConfigDao brushApkConfigDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.IChannelApkInfoService#selectAllAPkInfos()
	 */
	@Override
	public List<ApkRetentConfig> selectAllSimpleRetentApkInfos() {
		Object list = redisClient.get(CacheKey.ALL_APK_RETENT_CONFIG);
		if (list != null) {
			return (List<ApkRetentConfig>) list;
		} else {
			List<ApkRetentConfig> apkList = channelApkConfigDAO.selectAllRetentConfigs();
			redisClient.put(CacheKey.ALL_APK_RETENT_CONFIG, apkList, 120);
			return apkList;
		}
	}

	@Override
	public List<ChannelApkConfig> selectAllApkInfos() {
		Object obj = redisClient.get(CacheKey.APK_LIST);
		if (obj != null) {
			return (List<ChannelApkConfig>) obj;
		} else {
			List<ChannelApkConfig> list = channelApkConfigDAO.selectApkInfos(null);
			if (list != null) {
				redisClient.put(CacheKey.APK_LIST, list, 60);
			} else {
				redisClient.put(CacheKey.APK_LIST, new ArrayList<ChannelApkConfig>(), 60);
			}
			return list;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IChannelApkInfoService#selectApkInfos(org.jsbd.
	 * boss.domian.channel.ChannelApkConfig)
	 */
	@Override
	public List<ChannelApkConfig> selectApkInfos(ChannelApkConfig config) {
		return channelApkConfigDAO.selectApkInfos(config);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IChannelApkInfoService#selectChannelApkConfig(java
	 * .lang.String)
	 */
	@Override
	public ChannelApkConfig selectChannelApkConfig(String mark) {
		String key = CacheKey.APK_KEY + mark;
		ChannelApkConfig config = (ChannelApkConfig) ehcache.get(key);
		if (config == null) {
			config = (ChannelApkConfig) redisClient.get(key);
			if (config != null) {
				ehcache.put(key, config);
				return config;
			}
		}
		ChannelApkConfig query = new ChannelApkConfig();
		query.setMark(mark);
		List<ChannelApkConfig> list = channelApkConfigDAO.selectApkInfos(query);
		if (list != null && list.size() > 0) {
			config = list.get(0);
			redisClient.put(key, config, 5 * 60);
		}
		return config;
	}




	@Override
	public Integer insertApkConfig(ChannelApkConfig apkConfig) {
		return channelApkConfigDAO.insertConfig(apkConfig);
	}

	@Override
	@Transactional("transactionManagerCrack")
	public Integer updateApkConfig(MultipartFile[] files, ChannelApkConfig apkConfig) {
		// 更新正态模型
		try {
			ChannelApkConfigTimeModel model = new ChannelApkConfigTimeModel();
			model.setMark(apkConfig.getMark());
			model.setSpDayLimit(apkConfig.getDaylimit());
			model.setType(ConstantsCmp.add);
			channelApkLimitDAO.updateTimeModel(model);
		} catch (Exception e) {
			LOGGER.error("正态模型没有配置", e);
		}
		String updateMark = null;
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				MultipartFile file = files[i];
				FileUploadVo fileVo = fileManager.uploadFile(file);
				if (fileVo != null) {
					String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
					String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
					String filePath = fileVo.getFilePath();
					String md5 = fileVo.getMd5();
					if (StringUtils.isNotBlank(fileType)) {
						if (i == 0) {
							String packName = fileVo.getPackName();
							String packageName = apkConfig.getPackageName();
							if (StringUtils.isBlank(packName)) {
								if (StringUtils.isBlank(packageName)) {
									throw new RuntimeException("包名解析错误，请指定包名");
								}
								updateMark = packageName.trim() + "-" + md5;
							} else {
								updateMark = fileVo.getPackName() + "-" + md5;
							}
							apkConfig.setUpdateMark(updateMark);
							apkConfig.setApkPath(filePath);
							apkConfig.setApkSize(fileVo.getFileSize());
						} else if (i == 1) {
							apkConfig.setAddJarPath(filePath);
						} else if (i == 2) {
							apkConfig.setRetentJarPath(filePath);
						} else if (i == 3) {
							apkConfig.setAddScriptPath(filePath);
						} else if (i == 4) {
							apkConfig.setRetentScriptPath(filePath);
						}else if (i == 5) {
							apkConfig.setSysPatch(filePath);
						}else if (i == 6) {
							apkConfig.setSelfFile(filePath);
						}
					}
				}
			}
		}
		String mark = apkConfig.getMark();
		if (StringUtils.isNotBlank(updateMark)) {
			ChannelProvinceInfo channelProvinceInfo = new ChannelProvinceInfo();
			channelProvinceInfo.setMark(apkConfig.getMark());
			channelProvinceInfo.setUpdateMark(updateMark);
			channelProvicneInfoDAO.updateMark(channelProvinceInfo);
			mark = updateMark;
		}
		ApkSimulator apkSimulator = new ApkSimulator();
		apkSimulator.setMark(mark);
		apkSimulatorService.delApkSimulator(apkSimulator);
		updateApkSimulator(apkConfig, mark);
		redisClient.remove(CacheKey.APK_TIME_MODEL + mark);
		redisClient.remove(CacheKey.APK_KEY + mark);
		redisClient.remove(CacheKey.APK_LIST);
		return channelApkConfigDAO.updateConfig(apkConfig);
	}

	/**
	 * 
	 * updateApkSimulator:更新apk模拟器版本号
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param apkConfig
	 * @param mark
	 */
	private void updateApkSimulator(ChannelApkConfig apkConfig, String mark) {
		String sysVer = apkConfig.getSysVer();
		List<ApkSimulator> apkConfigList = new ArrayList<ApkSimulator>();
		if (StringUtils.isNotBlank(sysVer)) {
			String[] sysVers = sysVer.split(",");
			for (String ver : sysVers) {
				ApkSimulator apkSimulator = new ApkSimulator();
				apkSimulator.setMark(mark);
				apkSimulator.setSysVer(ver);
				apkConfigList.add(apkSimulator);
			}
		}
		apkSimulatorService.insertApkInfoList(apkConfigList);
	}

	@Override
	public List<ChannelApkConfig> getConfigList(ChannelApkConfig queryConfig) {
		return channelApkConfigDAO.selectApkConfigs(queryConfig);
	}

	@Override
	public List<ChannelApkConfig> queryApkInfos(ChannelApkConfig config) {
		return channelApkConfigDAO.queryApkInfos(config);
	}

	@Override
	public ChannelApkConfig selectApkConfig(String mark) {
		ChannelApkConfig config = new ChannelApkConfig();
		config.setMark(mark);
		List<ChannelApkConfig> list = channelApkConfigDAO.selectApkInfos(config);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public ChannelApkConfig selectApkConfigByid(int id) {
		ChannelApkConfig config = new ChannelApkConfig();
		config.setId(id);
		List<ChannelApkConfig> list = channelApkConfigDAO.selectApkInfos(config);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public int getCountOfConfig(ChannelApkConfig config) {
		return channelApkConfigDAO.getCountOfConfig(config);
	}

	@Override
	@Transactional("transactionManagerCrack")
	public Integer saveApkConfig(MultipartFile[] files, ChannelApkConfig apkConfig) {
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				MultipartFile file = files[i];
				FileUploadVo fileVo = fileManager.uploadFile(file);
				if (fileVo != null) {
					String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
					String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
					String filePath = fileVo.getFilePath();
					String md5 = fileVo.getMd5();
					if (StringUtils.isNotBlank(fileType)) {
						if (i == 0) {
							String packName = fileVo.getPackName();
							String packageName = apkConfig.getPackageName();
							if (StringUtils.isBlank(packName)) {
								if (StringUtils.isBlank(packageName)) {
									throw new RuntimeException("包名解析错误，请指定包名");
								}
								apkConfig.setMark(packageName.trim() + "-" + md5);
							} else {
								apkConfig.setMark(fileVo.getPackName() + "-" + md5);
							}
							apkConfig.setApkPath(filePath);
							apkConfig.setApkSize(fileVo.getFileSize());
						} else if (i == 1) {
							apkConfig.setAddJarPath(filePath);
						} else if (i == 2) {
							apkConfig.setRetentJarPath(filePath);
						} else if (i == 3) {
							apkConfig.setAddScriptPath(filePath);
						} else if (i == 4) {
							apkConfig.setRetentScriptPath(filePath);
						} else if (i == 5) {
							apkConfig.setSysPatch(filePath);
						}else if (i == 6) {
							apkConfig.setSelfFile(filePath);
						}
					}
				}
			}
		}
		updateApkSimulator(apkConfig, apkConfig.getMark());
		apkConfig.setIsAssignDevice(0);
		return channelApkConfigDAO.insertConfig(apkConfig);
	}

	@Override
	@Transactional("transactionManagerCrack")
	public Integer saveCopyApkConfig(MultipartFile[] files, ChannelApkConfig apkConfig) {
		String copyMark = apkConfig.getMark();
		Integer id = apkConfig.getId();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				MultipartFile file = files[i];
				FileUploadVo fileVo = fileManager.uploadFile(file);
				if (fileVo != null) {
					String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
					String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
					String filePath = fileVo.getFilePath();
					String md5 = fileVo.getMd5();
					if (StringUtils.isNotBlank(fileType)) {
						if (i == 0) {
							String packName = fileVo.getPackName();
							String packageName = apkConfig.getPackageName();
							if (StringUtils.isBlank(packName)) {
								if (StringUtils.isBlank(packageName)) {
									throw new RuntimeException("包名解析错误，请指定包名");
								}
								apkConfig.setMark(packageName.trim() + "-" + md5);
							} else {
								apkConfig.setMark(fileVo.getPackName() + "-" + md5);
							}
							apkConfig.setApkPath(filePath);
							apkConfig.setApkSize(fileVo.getFileSize());
						}
					}
				}
			}
		}
		updateApkSimulator(apkConfig, apkConfig.getMark());
		apkConfig.setIsAssignDevice(0);
		apkConfig.setCopyMark(copyMark);
		channelApkConfigDAO.insertConfig(apkConfig);
		ChannelApkConfig config = selectApkConfig(apkConfig.getMark());
		apkConfig.setCopyId(id);
		apkConfig.setId(config.getId());
		channelApkConfigDAO.copyConfig(apkConfig);
		return 1;
	}

	@Override
	public Integer delApkConfig(String mark) {
		return channelApkConfigDAO.deleteConfig(mark);
	}

	@Override
	public List<ApkLoginTypeConfig> selectApkLoginConfigList(String mark) {
		ChannelApkConfig config = new ChannelApkConfig();
		config.setMark(mark);
		List<ApkLoginTypeConfig> list = loginTypeService.selectApkLoginConfigNoCache(mark);
		if (list != null && list.size() > 0) {
			Iterator<ApkLoginTypeConfig> loginTypeIterator = list.iterator();
			while (loginTypeIterator.hasNext()) {
				ApkLoginTypeConfig loginTypeConfig = loginTypeIterator.next();
				LoginTypeEnum loginType = LoginTypeEnum.valueOf(loginTypeConfig.getLoginType());
				if (loginType != null) {
					loginTypeConfig.setLoginName(LoginTypeEnum.valueOf(loginTypeConfig.getLoginType()).getName());
					String loginKey = CacheKey.APK_LOGIN_KEY + DateUtil.getDaysOfTime() + mark;
					String fieldExecKey = loginType.getType() + "exectotal";
					String execloginTotal = redisClient.evalHget(loginKey, fieldExecKey);
					int execTotal = 0;
					if (execloginTotal != null) {
						execTotal = Integer.parseInt(execloginTotal.toString());
					}
					loginTypeConfig.setExecTotal(execTotal);
				} else {
					loginTypeIterator.remove();
				}
			}
		}
		return list;
	}

	@Override
	public void insertAndUpdateApkLoginTypeConfig(List<ApkLoginTypeConfig> list) {
		loginTypeService.insertAndUpdateApkLoginTypeConfig(list);
	}

	@Override
	public Integer updateApkConfig(ChannelApkConfig apkConfig) {
		redisClient.remove(CacheKey.APK_KEY + apkConfig.getMark());
		redisClient.remove(CacheKey.APK_LIST);
		redisClient.remove(CacheKey.APK_TIME_MODEL + apkConfig.getMark());

		String isDelete = apkConfig.getIsDelete();
		if(StringUtils.isNotBlank(isDelete) && isDelete.equals("1")){
			BrushApkConfig config1 = new BrushApkConfig();
			config1.setApkId(apkConfig.getId());
			brushApkConfigDao.deleteBrushApkConfig(config1);
		}

		return channelApkConfigDAO.updateConfig(apkConfig);
	}

	@Override
	public ApkRetentConfig selectRetentApkConfig(String mark, Integer retentDay) {
		String cacheKey = CacheKey.A_APK_RETENT_CONFIG + mark + "retent";
		ApkRetentConfig config = (ApkRetentConfig) redisClient.get(cacheKey);
		if(config != null){
			return  config;
		}else{
			ApkRetentConfig param = new ApkRetentConfig();
			param.setMark(mark);
			param.setRetentDay(retentDay);
			List<ApkRetentConfig> apkList =  channelApkConfigDAO.selectAllRetentAPkInfos(param);
			if (apkList != null && apkList.size() > 0) {
				ApkRetentConfig apkRetentConfig = apkList.get(0);
				redisClient.put(cacheKey, apkRetentConfig, 120);
				return apkRetentConfig;
			}
			return null;
		}
	}


	@Override
	public ApkRetentConfig selectRetentConfig(String mark, Integer retentMonth, Integer execTimes){
		String cacheKey = CacheKey.A_APK_RETENT_CONFIG + mark + "fill";
		ApkRetentConfig config = (ApkRetentConfig) redisClient.get(cacheKey);
		if(config != null){
			return  config;
		}else{
			RetentLogConfig param = new RetentLogConfig();
			param.setMark(mark);
			param.setRetentMonth(retentMonth);
			param.setExecTimes(execTimes);
			List<ApkRetentConfig> apkList =  channelApkConfigDAO.selectRetentFillApkList(param);
			if (apkList != null && apkList.size() > 0) {
				ApkRetentConfig apkRetentConfig = apkList.get(0);
				redisClient.put(cacheKey, apkRetentConfig, 120);
				return apkRetentConfig;
			}
			return null;
		}
	}

	@Override
	public List<ChannelApkConfig> selectApkConfigsAndSimulator() {
		return channelApkConfigDAO.selectApkConfigsAndSimulator();
	}

	@Override
	public List<ChannelApkConfig> queryApkList(ChannelApkConfig apkConfig) {
		return channelApkConfigDAO.queryApkList(apkConfig);
	}

	@Override
	public List<ChannelApkConfig> getExportMsg(List<ChannelApkConfig> list) {

		if (list != null && list.size() > 0) {
			for (ChannelApkConfig channelApkConfig : list) {
				// step 1 补全手机版本信息
				List<ApkSimulator> apklist = apkSimulatorDAO.getVerList(channelApkConfig.getMark());
				if (apklist != null && apklist.size() > 0) {
					for (ApkSimulator apkSimulator : apklist) {
						if (StringUtils.isNotBlank(channelApkConfig.getSysVer())) {
							channelApkConfig.setSysVer(channelApkConfig.getSysVer() + ";" + apkSimulator.getSysVer());
						} else {
							channelApkConfig.setSysVer(apkSimulator.getSysVer());
						}

					}
				}
				// step 2 补全登录信息
				List<ApkLoginTypeConfig> loginList = selectApkLoginConfigList(channelApkConfig.getMark());
				Collections.sort(loginList, new Comparator() {
					@Override
					public int compare(Object a, Object b) {
						int one = ((ApkLoginTypeConfig) a).getLoginType();
						int two = ((ApkLoginTypeConfig) b).getLoginType();
						return one - two;
					}
				});
				if (loginList != null && loginList.size() > 0) {
					for (ApkLoginTypeConfig apkLoginTypeConfig : loginList) {
						/*
						 * if (apkLoginTypeConfig.getRate() <= 0) { continue; }
						 */
						String msg = LoginTypeEnum.valueOf(apkLoginTypeConfig.getLoginType()).getName() + ":" + apkLoginTypeConfig.getRate();
						if (StringUtils.isNotBlank(channelApkConfig.getLoginTypeMSG())) {
							channelApkConfig.setLoginTypeMSG(channelApkConfig.getLoginTypeMSG() + ";" + msg);
						} else {
							channelApkConfig.setLoginTypeMSG(msg);
						}

					}
				}
				// step 3 补全基地信息
				try {
					channelApkConfig.setBaseMSG(ApkBaseEnum.valueOf(channelApkConfig.getBase()).getName());
				} catch (Exception e) {
					LOGGER.error("Cant find the base{[]} enum", channelApkConfig.getBase());
				}

				// step 4 发送短信类型信息
				/*try {
					channelApkConfig.setBaseMSG(ApkBaseEnum.valueOf(channelApkConfig.getBase()).getName());
				} catch (Exception e) {
					LOGGER.error("Cant find the base{[]} enum", channelApkConfig.getBase());
				}*/

				// step 5 留存限制 ,
				try {
					List<RetentFilterConfig> filterList = retentFilterConfigService.selectRetentFilterConfigList(channelApkConfig.getId());
					if (filterList != null && filterList.size() > 0) {
						for (RetentFilterConfig retentFilterConfig : filterList) {
							/*
							 * if (apkLoginTypeConfig.getRate() <= 0) {
							 * continue; }
							 */
							String msg = retentFilterConfig.getName() + ":" + retentFilterConfig.getPriority();
							if (StringUtils.isNotBlank(channelApkConfig.getRetentTypeMSG())) {
								channelApkConfig.setRetentTypeMSG(channelApkConfig.getRetentTypeMSG() + ";" + msg);
							} else {
								channelApkConfig.setRetentTypeMSG(msg);
							}

						}
					}
				} catch (Exception e) {
					LOGGER.error("Cant find the retent filter config of apkId:{[]}", channelApkConfig.getId());
				}

				// step 6 "指定机型
				try {
					List<ApkDevice> deviceList = deviceService.selectDevices(channelApkConfig.getId());
					if (deviceList != null && deviceList.size() > 0) {
						for (ApkDevice apkDevice : deviceList) {
							/*
							 * if (apkLoginTypeConfig.getRate() <= 0) {
							 * continue; }
							 */
							String msg = apkDevice.getHsman() + ":" + apkDevice.getRate();
							if (StringUtils.isNotBlank(channelApkConfig.getModelTypeMSG())) {
								channelApkConfig.setModelTypeMSG(channelApkConfig.getModelTypeMSG() + ";" + msg);
							} else {
								channelApkConfig.setModelTypeMSG(msg);
							}

						}
					}
				} catch (Exception e) {
					LOGGER.error("Cant find the model type msg of apkId:{[]}", channelApkConfig.getBase());
				}

				// step 短信信息
				try {
					channelApkConfig.setSmsTypeMSG(SmsSendTypeEnum.valueOf(channelApkConfig.getSendType()).getName());
				} catch (Exception e) {
					LOGGER.error("Cant find the sendType{[]} enum", channelApkConfig.getSendType());
				}

				if ("0".equals(channelApkConfig.getIsDelete())) {
					channelApkConfig.setIsDelete("启动");
				} else {
					channelApkConfig.setIsDelete("作废");
				}
				// step 是否拉活
				try {
					if (channelApkConfig.getIsBrush() != null && channelApkConfig.getIsBrush() == 1) {
						channelApkConfig.setBrushMsg("是");
					} else {
						channelApkConfig.setBrushMsg("否");
					}
				} catch (Exception e) {
					LOGGER.error("Cant find the brush info of apkId:{[]}", channelApkConfig.getId());
				}
				// step 是否补量
				try {
					if (channelApkConfig.getIsFill() != null && channelApkConfig.getIsFill() == 1) {
						channelApkConfig.setFillMsg("是");
					} else {
						channelApkConfig.setFillMsg("否");
					}
				} catch (Exception e) {
					LOGGER.error("Cant find the fill info of apkId:{[]}", channelApkConfig.getId());
				}
				// step 是否留存顺序执行
				try {
					if (channelApkConfig.getRetent24Filter() != null && channelApkConfig.getRetent24Filter() == 1) {
						channelApkConfig.setRetent24FilterMsg("是");
					} else {
						channelApkConfig.setRetent24FilterMsg("否");
					}
				} catch (Exception e) {
					LOGGER.error("Cant find the retent24Filter of apkId:{[]}", channelApkConfig.getId());
				}
			}

		}
		return list;
	}

	@Override
	public List<TasktimeDto> getTasktimeByBaseList() {
		return channelApkConfigDAO.selectTasktimeByBaseList();
	}

	@Override
	public Map<String, Object> selectAllRetentMarkInfo(DataGridModel page, RetentPayConfig retentPayConfig) {
		return channelApkConfigDAO.selectAllRetentMarkInfo(page, retentPayConfig);
	}

	@Override
	public void batchEditVerSubmit(String marks, String sysVer) {
		String[] markArr = marks.split(",");
		String[] sysVerArr = sysVer.split(",");
		for (String mark : markArr) {
			ApkSimulator apkSimulator = new ApkSimulator();
			ChannelApkConfig apkConfig = new ChannelApkConfig();
			apkSimulator.setMark(mark);
			apkConfig.setMark(mark);
			apkSimulatorService.delApkSimulator(apkSimulator);

			List<ApkSimulator> apkConfigList = new ArrayList<ApkSimulator>();
			for (String ver : sysVerArr) {
				ApkSimulator apkSimulator1 = new ApkSimulator();
				apkSimulator1.setMark(mark);
				apkSimulator1.setSysVer(ver);
				apkConfigList.add(apkSimulator1);
			}
			apkSimulatorService.insertApkInfoList(apkConfigList);
			channelApkConfigDAO.updateModifyTime(apkConfig);
		}
	}

	@Override
	public void batchEditFileSubmit(String marks, MultipartFile[] files) {
		String[] markArr = marks.split(",");
		String updateMark = null;
		for (String mark : markArr) {
			ChannelApkConfig apkConfig = new ChannelApkConfig();
			apkConfig.setMark(mark);
			if (files != null && files.length > 0) {
				for (int i = 0; i < files.length; i++) {
					MultipartFile file = files[i];
					FileUploadVo fileVo = fileManager.uploadFile(file);
					if (fileVo != null) {
						String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
						String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
						String filePath = fileVo.getFilePath();
						String md5 = fileVo.getMd5();
						if (StringUtils.isNotBlank(fileType)) {
							if (i == 0) {
								updateMark = fileVo.getPackName() + "-" + md5;
								apkConfig.setUpdateMark(updateMark);
								apkConfig.setApkPath(filePath);
								apkConfig.setApkSize(fileVo.getFileSize());
							} else if (i == 1) {
								apkConfig.setAddJarPath(filePath);
							} else if (i == 2) {
								apkConfig.setRetentJarPath(filePath);
							} else if (i == 3) {
								apkConfig.setAddScriptPath(filePath);
							} else if (i == 4) {
								apkConfig.setRetentScriptPath(filePath);
							}else if (i == 5) {
								apkConfig.setSysPatch(filePath);
							}else if (i == 6) {
								apkConfig.setSelfFile(filePath);
							}
						}
					}
				}
			}
			channelApkConfigDAO.batchUpdateConfig(apkConfig);
		}

	}

	@Override
	public List<ChannelApkConfig> getAllMarkAndApkname() {
		return channelApkConfigDAO.getAllMarkAndApkname();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.IChannelApkInfoService#selectAllSimpleApk()
	 */
	@Override
	public List<ChannelApkConfig> selectAllSimpleApk() {
		return channelApkConfigDAO.selectAllSimpleApk();
	}

	@Override
	public void batchUpdateApkConfig(MultipartFile[] files, ChannelApkConfig apkConfig) {
		// 更新正态模型
		try {
			ChannelApkConfigTimeModel model = new ChannelApkConfigTimeModel();
			model.setMark(apkConfig.getMark());
			model.setSpDayLimit(apkConfig.getDaylimit());
			model.setType(ConstantsCmp.add);
			channelApkLimitDAO.updateTimeModel(model);
		} catch (Exception e) {
			LOGGER.error("正态模型没有配置", e);
		}
		String updateMark = null;
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				MultipartFile file = files[i];
				FileUploadVo fileVo = fileManager.uploadFile(file);
				if (fileVo != null) {
					String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
					String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
					String filePath = fileVo.getFilePath();
					String md5 = fileVo.getMd5();
					if (StringUtils.isNotBlank(fileType)) {
						if (i == 0) {
							updateMark = fileVo.getPackName() + "-" + md5;
							apkConfig.setUpdateMark(updateMark);
							apkConfig.setApkPath(filePath);
							apkConfig.setApkSize(fileVo.getFileSize());
						} else if (i == 1) {
							apkConfig.setAddJarPath(filePath);
						} else if (i == 2) {
							apkConfig.setRetentJarPath(filePath);
						} else if (i == 3) {
							apkConfig.setAddScriptPath(filePath);
						} else if (i == 4) {
							apkConfig.setRetentScriptPath(filePath);
						}else if (i == 5) {
							apkConfig.setSysPatch(filePath);
						}else if (i == 6) {
							apkConfig.setSelfFile(filePath);
						}
					}
				}
			}
		}
		String mark = apkConfig.getMark();
		if (StringUtils.isNotBlank(updateMark)) {
			ChannelProvinceInfo channelProvinceInfo = new ChannelProvinceInfo();
			channelProvinceInfo.setMark(apkConfig.getMark());
			channelProvinceInfo.setUpdateMark(updateMark);
			channelProvicneInfoDAO.updateMark(channelProvinceInfo);
			mark = updateMark;
		}

		String sysVer = apkConfig.getSysVer();
		if (!StringUtils.isBlank(sysVer)) {
			ApkSimulator apkSimulator = new ApkSimulator();
			apkSimulator.setMark(mark);
			apkSimulatorService.delApkSimulator(apkSimulator);
			updateApkSimulator(apkConfig, mark);
		}

		redisClient.remove(CacheKey.APK_TIME_MODEL + mark);
		redisClient.remove(CacheKey.APK_KEY + mark);
		redisClient.remove(CacheKey.APK_LIST);
		channelApkConfigDAO.updateConfigForBatch(apkConfig);

	}

	@Override
	public List<ChannelApkConfigDto> getConfigApkListByIsdelete(Integer isDelete, List<String> keywords) {
		return channelApkConfigDAO.selectConfigApkListByIsdelete(isDelete, keywords);
	}

	@Override
	public List<ApkLoginTypeConfig> selectApkLoginConfigList() {
		List<ApkLoginTypeConfig> list = loginTypeService.selectApkLoginConfigNoCache();
		if (list != null && list.size() > 0) {
			Iterator<ApkLoginTypeConfig> loginTypeIterator = list.iterator();
			while (loginTypeIterator.hasNext()) {
				ApkLoginTypeConfig loginTypeConfig = loginTypeIterator.next();
				LoginTypeEnum loginType = LoginTypeEnum.valueOf(loginTypeConfig.getLoginType());
				if (loginType != null) {
					loginTypeConfig.setLoginName(LoginTypeEnum.valueOf(loginTypeConfig.getLoginType()).getName());
					loginTypeConfig.setExecTotal(0);
				} else {
					loginTypeIterator.remove();
				}
			}
		}
		return list;
	}
}
